/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.data.dto.FollowUpDetailsDto;
import org.gov.vis.data.gui.AddProgencyDialog;
import org.gov.vis.data.gui.AddUpdateFollowUpDetailsDialog;
import org.gov.vis.data.gui.EditProgencyDialog;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author NAGESH
 *
 */
public class AddUpdateFollowUpDetailsListener implements ActionListener,BaseListener{/*



	private EditProgencyDialog editProgencyDialog;
	final private ResourceBundle rb = Util.getResources();
	private AddUpdateFollowUpDetailsDialog editFollowUpDetailsDialog;
	private AddInsertFollowUpDetailsDialog followUpDetDialog;

	public AddUpdateFollowUpDetailsListener(AddUpdateFollowUpDetailsDialog editFollowUpDetailsDialog){

		this.editFollowUpDetailsDialog = editFollowUpDetailsDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		final FollowUpDetailsDto followUpDetailsDto = (FollowUpDetailsDto)_dtoObject;
		//if(followUpDetDialog != null){
		editFollowUpDetailsDialog.getTxtBxOthers().setText(followUpDetailsDto.getOther());
		editFollowUpDetailsDialog.getTxtBxBirthWeight().setText(followUpDetailsDto.getBirthWeight());
		editFollowUpDetailsDialog.getTxtBxMonthlyWt().setText(followUpDetailsDto.getMonthlyWeight());
		editFollowUpDetailsDialog.getTxtBxNutrition().setText(followUpDetailsDto.getNutrition());
		System.out.println(followUpDetailsDto.getDeWormingDate());
		editFollowUpDetailsDialog.getDtChoDateOfDeworming().setDate(followUpDetailsDto.getDeWormingDate());
		editFollowUpDetailsDialog.getDtChoDateOfDeticking().setDate(followUpDetailsDto.getDeTickingDate());
		editFollowUpDetailsDialog.getDtChoDateOfVaccination().setDate(followUpDetailsDto.getVaccinationDate());

		//}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{


			if( actionEvent.getActionCommand().equals("Add new Follow Up Details") ){
				 followUpDetDialog = new AddInsertFollowUpDetailsDialog(Main.getMainFrame(), "Follow UP Details", false);
				followUpDetDialog.setProgencyObject(editProgencyDialog);
			}
			if(actionEvent.getSource() instanceof VISHyperLink){
				actionEvent.getSource();
				VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
				FollowUpDetailsDto followUpDetailsDto = (FollowUpDetailsDto)visHyperLink.getObject();
				System.out.println("action from the object----"+followUpDetailsDto.getBirthWeight());
				editFollowUpDetailsDialog = new AddUpdateFollowUpDetailsDialog(Main.getMainFrame(), rb.getString("dialog_edit_prmt_semen_title"), false);
				setDtoDetailsToGui(followUpDetailsDto);


			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}


	public void getFollowUpDetails(){
		//Get all the follow up Details

		List<String> colLst = new ArrayList<String>();
		colLst.add("Deworming");
		colLst.add("Vaccination");
		colLst.add("Deticking");
		colLst.add("Others");
		colLst.add("Nutrition");
		colLst.add("Birth Wt");
		colLst.add("Monthly Wt");

		FollowUpDetailsDto followUpDetDto1 = new FollowUpDetailsDto();
		followUpDetDto1.setDeWormingDate(new Date());
		followUpDetDto1.setVaccinationDate(new Date());
		followUpDetDto1.setDeTickingDate(new Date());
		followUpDetDto1.setOther("Others");
		followUpDetDto1.setNutrition("Nutrition");
		followUpDetDto1.setBirthWeight("birth");
		followUpDetDto1.setMonthlyWeight("monthly");

		FollowUpDetailsDto followUpDetDto2 = new FollowUpDetailsDto();
		followUpDetDto2.setDeWormingDate(new Date());
		followUpDetDto2.setVaccinationDate(new Date());
		followUpDetDto2.setDeTickingDate(new Date());
		followUpDetDto2.setOther("Others2");
		followUpDetDto2.setNutrition("Nutrition2");
		followUpDetDto2.setBirthWeight("birth2");
		followUpDetDto2.setMonthlyWeight("monthly2");


		List<FollowUpDetailsDto> followUpLst = new ArrayList<FollowUpDetailsDto>();
		followUpLst.add(followUpDetDto1);
		followUpLst.add(followUpDetDto2);


		List<FollowUpDetailsDto> followUpLst = AddProgencyDialog.tempFollowUpLst;
		ArrayList<Object> dataList = new ArrayList<Object>(1);
		if(followUpLst != null && !followUpLst.isEmpty()){
			for(FollowUpDetailsDto dto:followUpLst){
				ArrayList<Object> lst = new ArrayList<Object>(1);
				//setting each property of the current Accessory object (row) to the list
				lst.add(dto);
				lst.add(dto.getDeWormingDate());
				lst.add(dto.getVaccinationDate());
				lst.add(dto.getDeTickingDate());
				lst.add(dto.getOther());
				lst.add(dto.getNutrition());
				lst.add(dto.getBirthWeight());
				lst.add(dto.getMonthlyWeight());
				dataList.add(lst);
			}//end for 
		}//end if
		VISDataGrid.createDataPanel(dataList, colLst, editProgencyDialog.getPnlFollowUpDetails(),this);
		editProgencyDialog.getTxtBxFarmerCode().requestFocus();

	}

 */



	private AddUpdateFollowUpDetailsDialog followUpDetailsDialog;
	private FollowUpDetailsDto followUpDetailsDto;
	//public static List<FollowUpDetailsDto> tempFollowUpLst = new ArrayList<FollowUpDetailsDto>();
	public HashMap<String, String> errorMsgs = new HashMap<String, String>();
	public ResourceBundle rb = Util.getResources();
	List<FollowUpDetailsDto> followUpLst = AddProgencyDialog.tempFollowUpLst;
	private boolean errorsFound;

	public AddUpdateFollowUpDetailsListener(AddUpdateFollowUpDetailsDialog followUpDetailsDialog){

		this.followUpDetailsDialog = followUpDetailsDialog;
	}


	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		followUpDetailsDto = (FollowUpDetailsDto)followUpDetailsDialog.getFollowUpDetailsObject();
		if(actionEvent.getActionCommand().equals("Update")){
			errorsFound = false;
			//followUpDetailsDto = (FollowUpDetailsDto)followUpDetailsDialog.getFollowUpDetailsObject();
			if(followUpDetailsDto != null){

				followUpLst.remove(followUpDetailsDto);
			}
			addFollowUpDetails();
			if(!errorsFound){
				AddProgencyDialog.tempFollowUpLst.add(followUpDetailsDto);
			followUpDetailsDialog.dispose();
			AddProgencyDialog addProgencyDialog = (AddProgencyDialog)followUpDetailsDialog.getProgencyObject();
			addProgencyDialog.refreshScreen();
			}
			//followUpDetailsDialog.dispose();
			//AddProgencyDialog addProgencyDialog = (AddProgencyDialog)followUpDetailsDialog.getProgencyObject();
			//addProgencyDialog.refreshScreen();
		}
		if(actionEvent.getActionCommand().equals("Delete")){
			/*followUpLst.remove(followUpDetailsDto);
			
			followUpDetailsDialog.dispose();
			AddProgencyDialog addProgencyDialog = (AddProgencyDialog)followUpDetailsDialog.getProgencyObject();
			addProgencyDialog.refreshScreen();
			*/
			int response = JOptionPane.showConfirmDialog(null, GuiConstants.DELETE_CONFIRM_MSG, "Confirm",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			if (response == JOptionPane.YES_OPTION) {	
				try {
					
					followUpLst.remove(followUpDetailsDto);
					
					followUpDetailsDialog.dispose();
					AddProgencyDialog addProgencyDialog = (AddProgencyDialog)followUpDetailsDialog.getProgencyObject();
					JOptionPane.showMessageDialog(addProgencyDialog, rb.getString(GuiConstants.DATA_DELETE_PROCUREMENTSEMEN_SUCCESS));
					addProgencyDialog.refreshScreen();
							
				} catch (Exception e) {
					List<String> errorMsgLst = new ArrayList<String>();
					errorMsgLst.add(e.getMessage());
					//errorMsgPanel.setErrorMessages(errorMsgLst);
				}
			} else if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
				return;
			}

		}
	
	}

	private void addFollowUpDetails() {
		// TODO Auto-generated method stub
		setGuiDetailsToDto(followUpDetailsDialog);

		//need to perform validations and save the FollowUpDetails
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		final AddUpdateFollowUpDetailsDialog _editFollowUpDialog = (AddUpdateFollowUpDetailsDialog)_guiObject;
		followUpDetailsDto = new FollowUpDetailsDto();
		if(_editFollowUpDialog.getDtChoDateOfDeworming().getDate() != null){
			followUpDetailsDto.setDeWormingDate(_editFollowUpDialog.getDtChoDateOfDeworming().getDate());
		}
		if(_editFollowUpDialog.getDtChoDateOfDeticking().getDate() != null){
			followUpDetailsDto.setDeTickingDate(_editFollowUpDialog.getDtChoDateOfDeticking().getDate());
		}
		if(_editFollowUpDialog.getDtChoDateOfVaccination().getDate() != null){
			followUpDetailsDto.setVaccinationDate(_editFollowUpDialog.getDtChoDateOfVaccination().getDate());
		}
		followUpDetailsDto.setOther(_editFollowUpDialog.getTxtBxOthers().getText());
		followUpDetailsDto.setNutrition(_editFollowUpDialog.getTxtBxNutrition().getText());
		followUpDetailsDto.setBirthWeight(_editFollowUpDialog.getTxtBxBirthWeight().getText());
		followUpDetailsDto.setMonthlyWeight(_editFollowUpDialog.getTxtBxMonthlyWt().getText());

		validate(followUpDetailsDto);
		//AddProgencyDialog.tempFollowUpLst.add(followUpDetailsDto);
		//followUpLst.add(addFollowUpDetailsDto);
	//tempFollowUpLst.add(addFollowUpDetailsDto);

	//ProgencyDto.setFollowUpDetails(tempFollowUpLst);
}

private void validate(FollowUpDetailsDto addFollowUpDetailsDto2) {

	try{
		String dewromingDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_dworing"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
		if(dewromingDateErr != null)
			errorMsgs.put(rb.getString("dialog_label_prgy_dworing").toUpperCase(), dewromingDateErr);
		String detickinggDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_deticking"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
		if(detickinggDateErr != null)
			errorMsgs.put(rb.getString("dialog_label_prgy_deticking").toUpperCase(), detickinggDateErr);

		String vaccinationDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_vaccintion"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
		if(vaccinationDateErr != null)
			errorMsgs.put(rb.getString("dialog_label_prgy_vaccintion").toUpperCase(), vaccinationDateErr);
		String monthWeightErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwmnthwgt"), addFollowUpDetailsDto2.getMonthlyWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
		if(monthWeightErr != null)
			errorMsgs.put(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase(), monthWeightErr);
		String birthWeightErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwbrthwgt"), addFollowUpDetailsDto2.getBirthWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
		if(birthWeightErr != null)
			errorMsgs.put(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase(), birthWeightErr);

		if(errorMsgs != null && !errorMsgs.isEmpty()){
			throw new Exception();
		}
	}catch (Exception e) {
		String errMsg = null;
		List<String> errorMsgLst = new ArrayList<String>();
		if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_dworing").toUpperCase())){
			followUpDetailsDialog.getLblDeworing().setForeground(redColor);
			errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_dworing").toUpperCase());
			errorMsgLst.add(errMsg);
		}
		if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_vaccintion").toUpperCase())){
			followUpDetailsDialog.getLblVaccinations().setForeground(redColor);
			errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_vaccintion").toUpperCase());
			errorMsgLst.add(errMsg);
		}
		if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_deticking").toUpperCase())){
			followUpDetailsDialog.getLblDeticking().setForeground(redColor);
			errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_deticking").toUpperCase());
			errorMsgLst.add(errMsg);
		}
		if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase())){
			followUpDetailsDialog.getLblBirthWt().setForeground(redColor);
			errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase());
			errorMsgLst.add(errMsg);
		}
		if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase())){
			followUpDetailsDialog.getLblMonthlyWt().setForeground(redColor);
			errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase());
			errorMsgLst.add(errMsg);
		}
		String errorMessages = "";
		Iterator<String> itr = errorMsgLst.iterator();
		while(itr.hasNext()){
			errMsg = itr.next();
			errorMessages += errMsg;

			if(!errMsg.contains("\n")){
				errorMessages += "\n";
			}
		}
		JOptionPane.showMessageDialog(followUpDetailsDialog, errorMessages);
		if(!errorMessages.equals("")){
			errorsFound  = true;
		}
	}
}


@Override
public void setDtoDetailsToGui(Object _dtoObject) {
	// TODO Auto-generated method stub

}


@Override
public void clearGUILabelColors(Color color) {
	followUpDetailsDialog.getLblBirthWt().setForeground(color);
	followUpDetailsDialog.getLblDeticking().setForeground(color);
	followUpDetailsDialog.getLblDeworing().setForeground(color);
	followUpDetailsDialog.getLblMonthlyWt().setForeground(color);
	followUpDetailsDialog.getLblNutrition().setForeground(color);
	followUpDetailsDialog.getLblOthers().setForeground(color);
	followUpDetailsDialog.getLblVaccinations().setForeground(color);

}


}
